package com.jetinno.syrup2;


import android.os.SystemClock;
import android.util.Log;

import com.jetinno.hex.HexTrans;
import com.jetinno.utils.LogUtils;
import com.jinuo.mhwang.serialport.BaseSerialPort;

import java.io.File;
import java.io.IOException;

public class SyrupCOM extends BaseSerialPort {

    private static final String TAG = "SirupCOM";
    private static final String BOZZ_SIRUP_PORT = "/dev/ttyS3";

    private static SyrupCOM mInstance = null;

    private SyrupCOM() {
        boolean a = open();
        Log.e("糖浆机初始化打开", a + "");
    }


    public static SyrupCOM getInstance() {
        if (null == mInstance) {
            synchronized (SyrupCOM.class) {
                if (mInstance == null) {
                    mInstance = new SyrupCOM();
                }
            }
        }
        return mInstance;
    }

    @Override
    public boolean write(String data) {
        LogUtils.e(TAG,"data:"+data);
        if (mOutputStream == null) {
            return false;
        }
        return write(HexTrans.hexString2Bytes(data));
    }

    @Override
    public synchronized boolean write(byte[] data) {
        if (mOutputStream == null) {
            return false;
        }
        try {
            mOutputStream.write(data);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private File file;

    @Override
    public boolean open() {
        file = new File(BOZZ_SIRUP_PORT);
        if (!file.exists()) {
            return false;
        }

        return super.openSerialPort(file, 57600, 0);
    }


    @Override
    public String receive() {
        byte[] data = receiveBytesNonBlocking();
        if (data == null) {
            return null;
        }
        String receive=HexTrans.bytes2HexString(data, data.length);
        LogUtils.e(TAG,"receive:"+receive);
        return receive;
    }

    public byte[] receiveBytesNonBlocking() {
        if (mInputStream == null) {
            LogUtils.logXlog(TAG, "receiveNonBlocking - " + SERIAL_UNCONNECT);
            return null;
        }
        //暂停200毫秒以等待数据返回
//        SystemClock.sleep(200);
        try {
            // 先判断是否有数据获取，防止inputStream.read阻塞
            if (mInputStream.available() > 0) {
                return super.readData(mInputStream);
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public synchronized byte[] receiveBytes() {
        if (mInputStream == null) {
            LogUtils.e("test", "mInputStream == null");
            return null;
        }
        return super.readData(mInputStream);

    }


    @Override
    public void close() {
        if (null == file || !file.exists()) {
            return;
        }
        super.closeSerialPort(file);
        file = null;
        mInstance = null;
    }

    /**
     * IO连接异常时，重新连接IO
     */
    public boolean reconnect() {
        close();
        SystemClock.sleep(500);
        return open();
    }
}
